/**
 * Created by hebo on 2017/4/1.
 */

var Dialog = new _dialog();
function _dialog(){

    //打开层的拟模态窗口
    this.openSwinDialog = function(ifmId, url, title, width, height, success){
        new parent.js_dialog({id:ifmId, url:url, title:title, type:"edit", width:width, height:height, success:success});
    };

    //打开模态窗口
    this.openModalDialog = function(url, width, height, arguments,explorerVersion,isChangeForBrowser,scrolls){
        if(isChangeForBrowser==null)isChangeForBrowser = true;
        if(explorerVersion==null)explorerVersion = 6;
        if(isChangeForBrowser){
            if(!_isIE6){
                if(explorerVersion == 6){
                    width = width - 6;
                    height = height - 54;
                }
            }else if(_isIE){
                if(explorerVersion == 7){
                    width = width + 6;
                    height = height + 54;
                }
            }
        }
        if(scrolls!='no'&&scrolls!='false'){
            scrolls = 'no';
        }else{
            scrolls = 'yes';
        }
        var features = "scroll:"+scrolls+";status:no;dialogWidth:" + width + "px;dialogHeight:" + height + "px;help:no";
        var returnValue = showModalDialog(url, arguments, features);
        _pageModalDialog = null;
        return returnValue;
    };

}

var js_Dialog = new _js_dialog();
function _js_dialog(){

    //关闭层方法
    this.close = function(seinId, value){
        var eles = $('#frame_container iframe');
        var i = 0 ;
        while(eles.get(i).style.display == 'none'){
            i++;
        }
        var obj = eles.get(i).contentWindow.window;
        obj.jsReturnValue = value;
        $("#swin_div_"+seinId+"_btn").click();
    }
}

var zIndex = 999990;
var js_dialog = function(options){
    options = jQuery.extend({
        appendDiv: document.body,
        success: null,
        url:"",
    }, options);

    //得到相关参数，如控件ID、显示信息、成功事件、显示title等等
    var eleId = options.id == null ? "swin_dialog" : options.id;
    var url = options.url;
    var successCallBack = (options.success != null && typeof(options.success) == "function") ? options.success : null;
    var k = 0;
    while(document.getElementById('swin_div_'+eleId+"_"+k)){
        k++;
    }
    var seinId = eleId+"_"+k
    var title = options.title == null ? "显示弹出层" : options.title;

    //得到剧中的数量
    var w = $(window).width();
    var h = $(window).height();
    var width = options.width == null ? (w-30) : options.width;
    var height = options.height == null ? (h-30) : options.height;
    var leftNum = parseInt(width,10)/2;
    var topNum = parseInt(height,10)/2;

    $(options.appendDiv).append($('<div id="shield_div_iframe_'+seinId+'" class="shield_base_div" style="z-index:'+(zIndex)+'; width: '+w+'px; height: '+h+'px;"><iframe frameborder="0" scrolling="no"></iframe></div>'));

    //创建显示层的控件
    var $_swin = $("<DIV>",{
        id:'swin_div_'+seinId,
        width:width+"px",
        height:height+"px"
    }).addClass("open_dialog_div").css({"margin-left":"-"+leftNum+"px","margin-top":"-"+topNum+"px", 'z-index':(zIndex+1)});
    zIndex = zIndex + 2;

    //存放内容的空间
    var html = new Array();
    html.push('<div class="top_head">');
//	html.push('<div id="swin_div_');
//	html.push(seinId);
//	html.push('_titleimg" class="title_img"></div>');
    html.push('<div id="swin_div_');
    html.push(seinId);
    html.push('_title" class="title">');
    html.push('</div>');
    html.push('<div id="swin_div_');
    html.push(seinId);
    html.push('_btn" class="close_btn"><i class="iconfont">&#xe725;</i></div>');
    html.push('</div>');
    html.push('<div id="swin_div_');
    html.push(seinId);
    html.push('_content" class="content_div">');
    html.push('</div>');
    html.push('</div>');
    $_swin.append($(html.join('')));

    //放到body中
    $(options.appendDiv).append($_swin);

    //设置内容信息框高度
    var contentHeight = height- 50;
    $("#swin_div_"+seinId+"_content").css("height", contentHeight+"px");

    //传入的是访问地址，弹出的是带iframe的层
    $("<iframe>",{
        id:"swin_div_"+seinId+'_iframe',
        src:url,
        width:"100%",
        height:"100%",
        frameBorder:"0",
        "scrolling":"no"
    }).load(function(){
        $(this).contents().find("#ifmId").val(seinId);
    }).appendTo("#swin_div_"+seinId+"_content");
    $("#swin_div_"+seinId+"_titleimg").css({"background":"url(common/css/dialog/editPage.png)"});
    $("#swin_div_"+seinId+"_title").html(title);

    //设置关闭按钮事件
    $("#swin_div_"+seinId+"_btn").click(function(){
        $(this).remove();
        $("#shield_div_iframe_"+seinId).remove();
        $_swin.remove();

        if(successCallBack != null){
            successCallBack();
        }
        return;
    })

    //显示
    $_swin.show();

}

var jsReturnValue = "jsReturnValue";
var Remind = new _remind();
function _remind(){

    //弹出确认框
    this.showConfirm = function(message, title,  success) {
        new parent.js_remind({id:"showConfirm", title:title, message:message, type:"confirm", width:600, height:250, success:success});
    }

    //弹出成功框
    this.showSuccess = function(message, success) {
        new parent.js_remind({id:"showSuccess", message:message, type:"success", width:600, height:250, success:success});
    }

    //弹出错误框
    this.showError = function(message, success) {
        new parent.js_remind({id:"showError", message:message, type:"error", width:600, height:250, success:success});
    }

}

//关闭弹出层
var jsDialog = new _jsdialog();
function _jsdialog(){
    this.close = function(returnValue){
        var seinId = $("#ifmId").val();
        window.parent.js_Dialog.close(seinId, returnValue);
    }
}
//显示错误对话框
function showMessage_alert_error(){
    Remind.showError("显示错误信息！");
}

//显示成功对话框
function showMessage_alert_success(){
    Remind.showSuccess("显示成功信息！");
}

//显示确认对话框
function showMessage_confirm(){
    Remind.showConfirm("显示确认信息！", "删除", function(){alert("执行确定后的操作！")});
}
